define(['app', 'js/directive/listTree', 'js/filter/tree2Arr', 'datetime', 'pagination', 'autocomplete', 'filesaver', 'editor', 'fileupload', 'js/service/agupload'], function(app) {
    'use strict';
    /*manage knowledgeproperty*/
    app.controller('Viewknowledgenode', function($scope, crmConst, dataFactoryCRM, $q, $compile, Upload, $http, $timeout, AgUpload) {
        $scope.knowledge_spot_id;
        $scope.knowledge_id;
        $scope.msg_type;
        $scope.loginuserid;
        $scope.user_id;
        $scope.firsttabmsg = {};
        $scope.thirdttabmsg = {};
        $scope.shownodepath;
        $scope.is_display;
        $scope.is_thirddisplay;
        // show node path

        $scope.shownode = function(k) {
            var deferred = $q.defer();
            dataFactoryCRM.postDataByAPI('Viewknowledgenode/ShowNode', { knowledge_spot_id: k }).then(function(d) {
                if (d.rcode == 1) {
                    deferred.resolve(d.data);
                } else {
                    deferred.reject('call function error');
                }
            });
            return deferred.promise;
        }

        // the right of user viewing
        $scope.UserViewRight = function(k) {
            var deferred = $q.defer();
            dataFactoryCRM.postDataByAPI('Viewknowledgenode/JudgeUserRight', { knowledge_spot_id: k }).then(function(d) {
                if (d.rcode == 1) {
                    deferred.resolve(d);
                } else {
                    deferred.reject('call function error');
                }
            });
            return deferred.promise;
        }

        // get knowledge node msg
        $scope.GetKnowledgeNodeMsg = function() {
            var arr = {};
            var address = window.location.href;
            arr = address.split("/");
            $scope.knowledge_id = arr[arr.length - 4];
            $scope.msg_type = arr[arr.length - 3];
            $scope.knowledge_spot_id = arr[arr.length - 2];
            $scope.user_id = arr[arr.length - 1];
            $scope.UserViewRight($scope.knowledge_spot_id).then(function(_d) {
                if (_d.exsit_right == 1) {
                    if ($scope.msg_type == 1) {
                        dataFactoryCRM.postDataByAPI('Viewknowledgenode/GetTypeMsg', { knowledgeId: $scope.knowledge_id }).then(function(d) {
                            if (d.rcode == 1) {
                                $scope.firsttabmsg = d.data;
                                $('#add_tempaltevalue').html(d.data.content);
                                $scope.files = d.callfun;
                                if (d.data.first_page_image) {
                                    $scope.is_display = 1;
                                } else {
                                    $scope.is_display = 2;
                                }
                            } else {
                                // 
                            }
                        });
                    } else if ($scope.msg_type == 2) {
                        // 
                        dataFactoryCRM.postDataByAPI('Viewknowledgenode/GetTypeMsg', { knowledgeId: $scope.knowledge_id }).then(function(d) {
                            if (d.rcode == 1) {
                                $scope.secondmsg = d.data;
                                $scope.secondtabfile = d.callfun;
                            } else {
                                // 
                            }
                        });
                    } else {
                        dataFactoryCRM.postDataByAPI('Viewknowledgenode/GetTypeMsg', { knowledgeId: $scope.knowledge_id }).then(function(d) {
                            if (d.rcode == 1) {
                                $scope.thirdttabmsg = d.data;
                                if (d.data.first_page_image) {
                                    $scope.is_thirddisplay = 1;
                                } else {
                                    $scope.is_thirddisplay = 2;
                                }
                            } else {
                                // 
                            }
                        });
                    }
                } else {
                    $('<div class="ui small modal modal2" style="text-align: center;"><div class="header">提示</div><div class="content"><i class="huge red warning outline icon"></i>' +
                        '<p style="padding-top: 15px;"></p>您没有权限进行查看</p>' + '</div></div>'
                    ).modal({ closable: false }).modal('show');
                }
            });

            $scope.shownode($scope.knowledge_spot_id).then(function(d) {
                $scope.shownodepath = d;
            });
        }

        $scope.GetKnowledgeNodeMsg();

        // get file name and extension name 
        $scope.GetFileName = function(k) {
            var deferred = $q.defer();
            dataFactoryCRM.getDataByAPI('Viewknowledgenode/GetFileName?knowledgefileId=' + k).then(function(d) {
                if (d.rcode == 1) {
                    deferred.resolve(d);
                } else {
                    deferred.reject('call function error');
                }
            });
            return deferred.promise;
        }

        // download second tab file
        $scope.DownSecondTabFile = function(k) {
            $scope.GetFileName(k).then(function(d) {
                var extensionname = d.extersion;
                var original_name = d.name;
                var _url = 'Viewknowledgenode/DownSecondTabFile?knowledgefileId=' + k;
                dataFactoryCRM.getDataByAPIS(_url).then(function(response) {
                    var blob = new Blob([response], {
                        type: "application/octet-stream"
                    });
                    saveAs(blob, original_name[0] + '.' + extensionname[1]);
                });
            })
        }
    })
});